﻿#region USING
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Collections;
using System.Reflection;
using System.ComponentModel;
#endregion


public class BYTE
{
    public static string ToHexString(byte[] b)
    {
        string str = "";
        for (int i = 0; i < b.Length; i++)
            str += b[i].ToString("X2");
        return str;
    }
    public static string ToShortString(BinaryReader r)
    {
        int l = r.ReadByte();
        return CONST.enc.GetString(r.ReadBytes(l));
    }

    public static string ToShortString(byte[] b)
    {
        int l = b[0]; //длина слова
        return CONST.enc.GetString(b, 1, b.Length - 1);
    }

    public static T ToStruct<T>(BinaryReader br) where T : struct
    {
        return ToStruct<T>(br.ReadBytes(Marshal.SizeOf(typeof(T))));
    }

    public static T ToStruct<T>(byte[] b) where T : struct
    {
        IntPtr ptr = Marshal.AllocHGlobal(b.Length);
        Marshal.Copy(b, 0, ptr, b.Length);
        T obj = (T)Marshal.PtrToStructure(ptr, typeof(T));
        Marshal.FreeHGlobal(ptr);
        return obj;
    }

    public static byte[] Concat(byte[] a, byte[] b)
    {
        byte[] buffer1 = new byte[a.Length + b.Length];
        Buffer.BlockCopy(a, 0, buffer1, 0, a.Length);
        Buffer.BlockCopy(b, 0, buffer1, a.Length, b.Length);
        return buffer1;
    }
    public static byte[] Concat(byte[] a, byte[] b, byte[] c)
    {
        Array.Resize(ref a, a.Length + b.Length + c.Length);
        for (int i = 0; i < b.Length; i++)
            a[a.Length - (b.Length + c.Length) + i] = b[i];
        for (int i = 0; i < c.Length; i++)
            a[a.Length - c.Length + i] = c[i];
        return a;
    }
}

static public class CONST
{
    static UTF8Encoding _enc;
    public static UTF8Encoding enc { get { if (_enc == null) _enc = new System.Text.UTF8Encoding(); return _enc; } }
}

public class FILE
{
    public static List<FileInfo> GetDirInfo(string _exten)
    {
        DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
        List<FileInfo> list = new List<FileInfo>();
        foreach (FileInfo fiTemp in di.GetFiles())
            if (fiTemp.Extension == _exten)
                list.Add(fiTemp);
        list.Sort(Compare);
        //var res = from dt in list orderby dt.LastWriteTime ascending select dt;
       

        return list;
    }

    static int Compare(FileInfo x, FileInfo y)
    {
        if (x.LastWriteTime > y.LastWriteTime)
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}

public static class ToBYTE
{
    /// <summary>Для слов до 255/2 символов. Первый байт - длина слова в байтах, далее упаковка через enc.GetBytes</summary>
    public static byte[] ShortString(string str)
    {
        if (str == null) str = "";
        byte[] b = CONST.enc.GetBytes(str);
        if (b.Length > 255) throw new Exception("Словo [" + str + "] не может быть упаковано  - оно слишком длинное");
        return BYTE.Concat(new byte[] { (byte)b.Length }, b);
    }

    public static byte[] Struct<T>(T source) where T : struct
    {
        int len = Marshal.SizeOf(source);

        byte[] b = new byte[len];
        IntPtr ptr = Marshal.AllocHGlobal(len);
        Marshal.StructureToPtr(source, ptr, true);
        Marshal.Copy(ptr, b, 0, len);
        Marshal.FreeHGlobal(ptr);
        return b;
    }
}

public static class Cripto
{
    public static SHA1 sha = new SHA1CryptoServiceProvider();
}